package com.eastfair.certificate.util;

import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;

@Component
public class FrameMarker {


    /**
     * 生成doc文档
     *
     * @param ftlName
     * @param dataMap
     * @param docName
     * @return
     */
    public void exportFile(String ftlName, Map dataMap, String docName, HttpServletResponse response) {
        Configuration config = new Configuration();
        String dir = System.getProperty("java.io.tmpdir");
        File file = new File(dir + File.separator + docName);
        try {
            String path = this.getClass().getClassLoader().getResource("template").getPath();//注意getResource("")里面是空字符串
            config.setDirectoryForTemplateLoading(new File(path));
            config.setObjectWrapper(new DefaultObjectWrapper());
            Template template = config.getTemplate(ftlName, "UTF-8");
            if (!file.exists()) {
                file.createNewFile();
            }
            Writer out = new BufferedWriter(new FileWriter(file));
            template.process(dataMap, out);
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (file.exists()) {
            BufferedInputStream bufferedInputStream = null;
            BufferedOutputStream bufferedOutputStream = null;
            try {
                // 清除buffer缓存
                response.reset();
                // 指定下载的文件名
                response.setHeader("Content-Disposition",
                        "attachment;filename=" + docName);
                response.setContentType("application/vnd.ms-excel;charset=UTF-8");
                response.setHeader("Pragma", "no-cache");
                response.setHeader("Cache-Control", "no-cache");
                FileInputStream inputStream = new FileInputStream(file);
                bufferedInputStream = new BufferedInputStream(inputStream); //缓冲流加速读
                OutputStream outputStream = response.getOutputStream();
                bufferedOutputStream = new BufferedOutputStream(outputStream);  //缓冲流加速写
                int n;
                while ((n = bufferedInputStream.read()) != -1) {
                    bufferedOutputStream.write(n);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    bufferedOutputStream.close();
                    bufferedInputStream.close();
                    file.delete();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) {
        System.out.println(System.getProperty("java.io.tmpdir"));
    }
}